class BooksController < ApplicationController

  def select_user
    @book = Book.find(params[:id])
    session[:rental] = Rental.new
    session[:rental].book_id = @book.id
    session[:rental].book_title = @book.title
  end

  def rent    
    @user = User.find(params[:user][:id])
    @rental = Rental.new
    @rental.book_id = session[:rental].book_id
    @rental.book_title = session[:rental].book_title    
    @rental.user_id = @user.id
    @rental.user_name = @user.name
    @rental.pick_up = Time.now
    @rental.save
    @book = Book.find(@rental.book_id)
    Book.update(@rental.book_id, { :quantity => subtract_quantity(@book) })
    flash[:notice] = 'Empréstimo realizado.'
    redirect_to :controller => 'books', :action => :index
  end

  def list_books
    @book = Book.new(params[:book])
    session[:books] = Book.find(:all,
                                :conditions => ["title LIKE ? AND author LIKE ? AND
                                                 publisher LIKE ? AND category LIKE ?",
                                                "%#{@book.title}%", "%#{@book.author}%",
                                                "%#{@book.publisher}%", "%#{@book.category}%" ],
                                :order => :title)
    
    if session[:books].length == 0
      flash[:notice] = 'Nenhum livro encontrado. Por favor, realize a pesquisa novamente.'
      redirect_to :controller => 'books', :action => :index
    end
  end

  def subtract_quantity(book)
    book.quantity -= 1
    book.quantity
  end

  def add_quantity(book)
    book.quantity += 1
    book.quantity
  end

  # GET /books
  # GET /books.xml
  def index
    @books = Book.find(:all, :order => :title)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @books }
    end
  end

  # GET /books/1
  # GET /books/1.xml
  def show
    @book = Book.find(params[:id])

    session[:book] = Book.find(@book.id)
    session[:comments] = Comment.find_all_by_book_id(@book.id)
    session[:rentals] = Rental.find_all_by_book_id(@book.id)

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/new
  # GET /books/new.xml
  def new
    @book = Book.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @book }
    end
  end

  # GET /books/1/edit
  def edit
    @book = Book.find(params[:id])

    session[:comments] = Comment.find_all_by_book_id(@book.id)
  end

  # POST /books
  # POST /books.xml
  def create
    @book = Book.new(params[:book])

    respond_to do |format|
      if @book.save
        flash[:notice] = 'Livro criado com sucesso.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created, :location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /books/1
  # PUT /books/1.xml
  def update
    @book = Book.find(params[:id])

    respond_to do |format|
      if @book.update_attributes(params[:book])
        flash[:notice] = 'Livro atualizado com sucesso.'
        format.html { redirect_to(@book) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.xml
  def destroy
    @book = Book.find(params[:id])
    @book.destroy

    respond_to do |format|
      format.html { redirect_to(books_url) }
      format.xml  { head :ok }
    end
  end
end
